package com.itheima.Service.CheckServicelmpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.Service.CheckgroupService;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.exception.checkitemException;
import com.itheima.mapper.CheckgroupCheckitemMapper;
import com.itheima.mapper.CheckgroupMapper;
import com.itheima.mapper.CheckitemMapper;
import com.itheima.mapper.SetmealCheckgroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckGroupDEV;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CheckgroupServiceilmp implements CheckgroupService {
    @Autowired
    private CheckgroupMapper checkgroupMapper;
    @Autowired
    private CheckgroupCheckitemMapper checkgroupCheckitemMapper;
    @Autowired
    private SetmealCheckgroupMapper setmealCheckgroupMapper;
    @Autowired
    private CheckitemMapper checkitemMapper;
    /**
     * 新增检查组
     */
    @Transactional
    public void list(CheckGroup checkGroup,List<Integer>checkitemIds) {
        CheckGroupDEV checkGroupDEV = new CheckGroupDEV();
        BeanUtils.copyProperties(checkGroup,checkGroupDEV);
        checkgroupMapper.list(checkGroupDEV);
        Integer id = checkGroupDEV.getId();
        for (Integer checkitemId : checkitemIds) {
            checkgroupCheckitemMapper.insert(id,checkitemId);
        }

    }

    /**
     * 分页查询检查组
     * @param queryPageBean
     * @return
     */
    public PageResult pagelist(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //根据项目名字分页查询
        CheckGroup checkGroup = new CheckGroup();
        checkGroup.setName(queryPageBean.getQueryString());
        BeanUtils.copyProperties(queryPageBean,checkGroup);
        Page<CheckGroup>P =checkgroupMapper.pagelist(checkGroup);
        return new PageResult(P.getTotal(),P.getResult());
    }

    /**
     * 删除检查组
     * @param id
     */
    public void delect(Integer id) {
        CheckGroup checkGroup = checkgroupMapper.readcheckgoup(id);
        List<Integer>listcheckgroup=setmealCheckgroupMapper.readsteam(checkGroup.getId());
        //存在关联的套餐,不行删除
        if (listcheckgroup!=null && listcheckgroup.size()>0){
            throw new checkitemException(MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        checkgroupMapper.delect(id);
        setmealCheckgroupMapper.delectcheckgroup(id);

    }

    /**
     * 根据套餐id查询检查组
     * @param setmealId
     * @return
     */
    public List<Integer> readsteaml(Integer setmealId) {
        return setmealCheckgroupMapper.list(setmealId);
    }

    /**
     * 查询所有检查组
     * @return
     */
    public List<CheckGroup> readgroup() {
        return checkgroupMapper.readgroup();
    }

    /**
     * 根据id查询检查组
     * @param id
     * @return
     */
    public CheckGroup readall(Integer id) {
        return checkgroupMapper.readcheckgoup(id);
    }

    /**
     * 修改检查组
     * @param checkGroup
     * @param checkitemIds
     */
    @Transactional
    public void updatecheck(CheckGroup checkGroup,List<Integer>checkitemIds) {
        checkgroupMapper.UPATE(checkGroup);
        Integer id = checkGroup.getId();
        //先全部删除
        checkgroupCheckitemMapper.delectcheckgroup(id);
        //重新插入
        for (Integer checkitemId : checkitemIds) {
            checkgroupCheckitemMapper.insert(id,checkitemId);
        }
    }
}
